package com.chexiehui.jc.utils;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.Glide;

import java.io.File;
import java.math.BigDecimal;

/**
 * 缓存管理器
 * Created by SongJun on 2016/6/23.
 */
public class DataCleanManager {

    /**
     * 获取缓存大小
     * @param context
     * @return
     * @throws Exception
     */
    public static String getTotalCacheSize(Context context) throws Exception {
        long cacheSize = getFolderSize(context.getCacheDir());
         cacheSize += getFolderSize(Glide.getPhotoCacheDir(context));
         cacheSize += getFolderSize(context.getFilesDir());
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            cacheSize += getFolderSize(context.getExternalCacheDir());
        }
        return getFormatSize(cacheSize);
    }

    /**
     * 清除缓存
     * @param context
     */
    public static void clearAllCache(Context context) {
        deleteDir(context.getCacheDir());
        deleteDir(Glide.getPhotoCacheDir(context));
        deleteDir(context.getFilesDir());
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            deleteDir(context.getExternalCacheDir());
        }
    }

    private static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }

    // 获取文件大小
    //Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录，一般放一些长时间保存的数据
    //Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录，一般存放临时缓存数据
    public static long getFolderSize(File file) throws Exception {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                // 如果下面还有文件
                if (fileList[i].isDirectory()) {
                    size = size + getFolderSize(fileList[i]);
                } else {
                    size = size + fileList[i].length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 格式化单位
     * @param size
     * @return
     */
    public static String getFormatSize(long size) {
        BigDecimal kiloByte = BigDecimal.valueOf(size).divide(BigDecimal.valueOf(1024), 2, BigDecimal.ROUND_HALF_UP);
        if (kiloByte.compareTo(BigDecimal.valueOf(1)) < 0) {
            return size + "B";
        }
        BigDecimal megaByte = kiloByte.divide(BigDecimal.valueOf(1024), 2, BigDecimal.ROUND_HALF_UP);
        if (megaByte.compareTo(BigDecimal.valueOf(1)) < 0) {
            return kiloByte.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "KB";
        }
        BigDecimal gigaByte = megaByte.divide(BigDecimal.valueOf(1024), 2, BigDecimal.ROUND_HALF_UP);
        if (gigaByte.compareTo(BigDecimal.valueOf(1)) < 0) {
            return megaByte.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "MB";
        }

        BigDecimal teraBytes = gigaByte.divide(BigDecimal.valueOf(1024), 2, BigDecimal.ROUND_HALF_UP);
        if (teraBytes.compareTo(BigDecimal.valueOf(1)) < 0) {
            return gigaByte.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "GB";
        }
        return teraBytes.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB";
    }
}
